
#include <BStream>
#include <BStyle>
#include "member_BGroupBox.h"
#include "member_BWidget.h"

using namespace BWE;

member_BGroupBox::member_BGroupBox(BGroupBox* groupBox)
{
	boss = groupBox;
	flat = false;
	wired = true;
	titleVisible = true;
	titleSpace = 1;
	titleSize = 0;
	titleStyle = 0;
	folded = false;
	checkable = false;
	checked = false;
	ia_align = Align_LeftCenter;
	ia_spacing = 2;
	wireSize = 1.0f;
	wireStripe = 0xFFFF;
	autoTop = true;
	autoLeft = true;
	autoRight = true;
	autoBottom = true;

	dirty = true;
	hovered = false;
	pressed = false;
}
member_BGroupBox::~member_BGroupBox()
{

}

void member_BGroupBox::freshRects()
{
	const BStyle* style = boss->realStyle();
	const BFont* titleFont = 0;
	if (titleStyle)
		titleFont = titleStyle->value(Value_Font);
	if (!titleFont)
		titleFont = style->font();

	if (widget_member(boss)->t_title.size())
		ia_textSize = titleFont->textSize(widget_member(boss)->t_title);
	else
		ia_textSize = titleFont->textSize(widget_member(boss)->title);

	BIcon* icon = boss->icon();
	if (icon || ia_form > Form_TextOnly || checkable)
	{
		ia_iconSize = iconSize;
		if (ia_iconSize.width() < 1)
			ia_iconSize.width() = ia_textSize.height();
		if (ia_iconSize.height() < 1)
			ia_iconSize.height() = ia_textSize.height();
	}

	if (titleVisible)
	{
		BPerch perch = boss->perch();
		if (autoTop)
		{
			perch.top() = boss->preferSize(Part_Top).height();
			int realTitleHeight = bMax(titleSize, titleIconSize.height());
			if (realTitleHeight < titleFont->size())
				realTitleHeight = titleFont->size();
			if (realTitleHeight < ia_textSize.height())
				realTitleHeight = ia_textSize.height();
			if (realTitleHeight < ia_iconSize.height())
				realTitleHeight = ia_iconSize.height();
			realTitleHeight += titleMargin.height();
			if (perch.top() < realTitleHeight)
				perch.top() = realTitleHeight;
		}
		if (!autoTop)
			perch.top() = style->value(Value_Perch_Top);
		if (!autoLeft)
			perch.left() = style->value(Value_Perch_Left);
		if (!autoRight)
			perch.right() = style->value(Value_Perch_Right);
		if (!autoBottom)
			perch.bottom() = style->value(Value_Perch_Bottom);
		if (!flat)
		{
			int halfTop = perch.top() / 2;
			if (autoLeft)
			{
				perch.left() = boss->preferSize(Part_Left).width();
				if (perch.left() < halfTop)
					perch.left() = halfTop;
			}
			if (autoRight)
			{
				perch.right() = boss->preferSize(Part_Right).width();
				if (perch.right() < halfTop)
					perch.right() = halfTop;
			}
			if (autoBottom)
			{
				perch.bottom() = boss->preferSize(Part_Bottom).height();
				if (perch.bottom() < halfTop)
					perch.bottom() = halfTop;
				if (folded)
					perch.bottom() = 0;
			}
		}
		boss->setPerch(perch);
		if (folded)
		{
			backupPerch = perch;
		}
	}

	centerSize.reset();
	for (int i = 0; i < boss->layoutCount(); i++)
	{
		BLayout* layout = boss->layout(i);
		if (layout->part() & Part_Top)
			continue;
		layout->setVisible(!folded);
		if (layout->part() == Part_Center)
		{
			BSize psize = layout->preferSize();
			if (centerSize.width() < psize.width())
				centerSize.width() = psize.width();
			if (centerSize.height() < psize.height())
				centerSize.height() = psize.height();
		}
		else if (layout->part() == Part_LeftCenter)
		{
			BSize psize = layout->preferSize();
			if (centerSize.height() < psize.height())
				centerSize.height() = psize.height();
		}
		else if (layout->part() == Part_RightCenter)
		{
			BSize psize = layout->preferSize();
			if (centerSize.height() < psize.height())
				centerSize.height() = psize.height();
		}
		else if (layout->part() == Part_BottomCenter)
		{
			BSize psize = layout->preferSize();
			if (centerSize.width() < psize.width())
				centerSize.width() = psize.width();
		}
	}

	titleRect = boss->rect(Part_Top);
	titleRect.top() += titleMargin.top();
	titleRect.left() += titleMargin.left();
	titleRect.right() -= titleMargin.right();
	titleRect.bottom() -= titleMargin.bottom();

	headerRect = adjust(titleRect);
	headerRect.top() -= titleMargin.top();
	headerRect.left() -= titleMargin.left();
	headerRect.right() += titleMargin.right();
	headerRect.bottom() += titleMargin.bottom();

	int border = boss->border();
	BSize size = boss->size();
	BPerch perch = boss->perch();

	BRectf wireRect;
	wireRect.left() = border + perch.left() / 2.0f;
	wireRect.top() = border + perch.top() / 2.0f;
	wireRect.right() = size.width() - border - perch.right() / 2.0f;
	wireRect.bottom() = size.height() - border - perch.bottom() / 2.0f;
	if (IRound* round = widget_member(boss)->round)
	{
		wirePoints.reset();
		wirePoints.append(BPointf((float)headerRect.right(), wireRect.top()));
		round->createRightTopPoints(wireRect, wirePoints);
		round->createRightBottomPoints(wireRect, wirePoints);
		round->createLeftBottomPoints(wireRect, wirePoints);
		round->createLeftTopPoints(wireRect, wirePoints);
		wirePoints.append(BPointf((float)headerRect.left(), wireRect.top()));
	}
	else
	{
		wirePoints.reset();
		wirePoints.append(BPointf((float)headerRect.left(), wireRect.top()));
		wirePoints.append(wireRect.leftTop());
		wirePoints.append(wireRect.leftBottom());
		wirePoints.append(wireRect.rightBottom());
		wirePoints.append(wireRect.rightTop());
		wirePoints.append(BPointf((float)headerRect.right(), wireRect.top()));
	}
}
